public class Solution19 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode first=new ListNode(-1,head);
        ListNode fast=first;
        for (int i=0;i<=n;i++){
            fast=fast.next;
        }
        ListNode slow=first;
        while (fast!=null){
            slow=slow.next;
            fast=fast.next;
        }
        slow.next=slow.next.next;
        return first.next;
    }

    public static void main(String[] args) {
        System.out.println(new Solution19().removeNthFromEnd(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5))))),5).next.next.next.val);
    }
}
